//
//  GGCalendarView.m
//  ggtool
//
//  Created by engfei on 2017/4/26.
//  Copyright © 2017年 GaiGai. All rights reserved.
//

#import "GGCalendarLogicView.h"

@interface GGCalendarTitleView ()
{
    CGFloat kRationH;
}
@property (nonatomic, strong) UIFont *defaultTitleFont;
@property (nonatomic, strong) UIFont *defaultDetailFont;

@end

@implementation GGCalendarTitleView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setInitlize];
    }
    return self;
}

- (instancetype)init {
    if (self = [super init]) {
        [self setInitlize];
    }
    return self;
}

// 初始化
- (void)setInitlize {
    
    kRationH = 2;
    _defaultTitleFont = [UIFont systemFontOfSize:14];
    _defaultDetailFont = [UIFont systemFontOfSize:11];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    [self addSubview:titleLabel];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = _defaultTitleFont;
    
    self.titleLabel = titleLabel;
    
    UILabel *detailLabel = [[UILabel alloc] init];
    [self addSubview:detailLabel];
    detailLabel.textAlignment = NSTextAlignmentCenter;
    detailLabel.hidden = YES;
    detailLabel.font = _defaultDetailFont;
    self.detailLabel = detailLabel;
 
}

- (void)setRatioH:(CGFloat)ratioH {
    _ratioH = ratioH;
    kRationH = _ratioH;
}

- (void)setIsHiddenDetail:(BOOL)isHiddenDetail {
    _isHiddenDetail = isHiddenDetail;
    self.detailLabel.hidden = _isHiddenDetail;
}

- (void)setSelected:(BOOL)selected {
    _selected = selected;
    
    if (self.selectedColor == nil) {
        self.selectedColor = [UIColor colorWithRed:70/255.0f green:171/255.0f blue:179/255.0f alpha:1];;
    }
    if (selected) {
        self.backgroundColor = self.selectedColor;
        self.titleLabel.textColor = self.selectedColor;
        self.detailLabel.textColor = self.selectedColor;
    } else {
        self.backgroundColor = [UIColor whiteColor];
        self.titleLabel.textColor = [UIColor blackColor];
        self.detailLabel.textColor = [UIColor blackColor];
    }
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    __weak typeof(self) weakSelf = self;
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(weakSelf);
        make.height.mas_equalTo(weakSelf.frame.size.height / (kRationH + 1) * kRationH);
    }];
    
    [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(weakSelf);
        make.top.equalTo(weakSelf.titleLabel.mas_bottom);
    }];
    
}

- (void)willMoveToSuperview:(UIView *)newSuperview {
    [super willMoveToSuperview:newSuperview];

}

- (void)didMoveToSuperview {
    [super didMoveToSuperview];
    
}



@end

@interface GGCalendarLogicView ()

@property (nonatomic, assign) NSUInteger rows;
@property (nonatomic, assign) CGFloat rowHeight;
@property (nonatomic, assign) CGFloat columnWidth;
// 缓存池
@property (nonatomic, strong) NSMutableArray *titleM;

@property (nonatomic, assign) NSInteger selectedRow;
@property (nonatomic, assign) NSInteger selectedColumn;
@property (nonatomic, strong) GGCalendarTitleView *selectedTitleView;

@end

@implementation GGCalendarLogicView

- (NSMutableArray *)titleM {
    if (_titleM == nil) {
        _titleM = [NSMutableArray array];
    }
    return _titleM;
}

- (instancetype)init {
    if (self = [super init]) {
        [self setInitialize];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setInitialize];
    }
    return self;
}

- (void)setInitialize {
    _rowHeight = 44.0f;
    
    
}

- (void)reloadData {
    
    if (self.dataSource == nil) {
        return;
    }
    
    self.rows = [self.dataSource numberOfRowsCalendarView:self];
    if ([self.dataSource respondsToSelector:@selector(heightForRowtitleView:)]) {
        self.rowHeight = [self.dataSource heightForRowtitleView:self];
    }
    
    self.columnWidth = self.bounds.size.width / 7;
    
    // 按顺序创建
    for (int i = 0; i < self.rows; i++) {
        for (int j = 0; j < 7; j++) {
            GGCalendarTitleView *titleView = [self.dataSource titleView:self forRow:i column:j];
            titleView.hidden = NO;
            
            if (titleView.selected) {
                self.selectedRow = i;
                self.selectedColumn = j;
                self.selectedTitleView = titleView;
            }
            
        }
    }
    
    CGRect frame = self.frame;
    frame.size.height = self.rowHeight * self.rows;
    self.frame = frame;
    // 多余的隐藏
    if (self.titleM.count > self.rows * 7) {
        for (NSUInteger i = self.rows * 7; i < self.titleM.count; i++) {
            GGCalendarTitleView *titleView = self.titleM[i];
            titleView.hidden = YES;
        }
    }
  
}

// 按顺序创建
- (GGCalendarTitleView *)dequeueReusableTitleViewAtRow:(NSUInteger)row column:(NSUInteger)column {
    
    GGCalendarTitleView *titleView = nil;
    NSLog(@"row = %zd, column = %zd", row, column);
    NSLog(@"%zd", self.titleM.count);
    if (self.titleM.count < (row  * 7 + column + 1)) {
        
        CGFloat y = row * self.rowHeight;
        CGFloat x = column * self.columnWidth;
        
        titleView = [[GGCalendarTitleView alloc] init];
        titleView.row = row;
        titleView.column = column;
        titleView.selected = NO;
        titleView.frame = CGRectMake(x, y, self.columnWidth, self.rowHeight);
        [self addSubview:titleView];
        [self.titleM addObject:titleView];
        
    } else {
        titleView = self.titleM[row * 7 + column];
    }
    
    return titleView;
    
}


- (NSUInteger)numberOfRows {
    return self.rows;
}
- (GGCalendarTitleView *)titleForRow:(NSUInteger)row column:(NSUInteger)column {
    
    NSUInteger index = row * 7 + column;
    if (index + 1 > self.titleM.count) return nil;
    return self.titleM[index];
}

- (CGFloat)viewHeight {
    return self.rowHeight * self.rows;
}




@end
